package com.sluggard.security.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sluggard.security.manage.mapper.DictDataMapper;
import com.sluggard.security.manage.model.entity.DictDataEntity;
import com.sluggard.security.manage.model.req.DictTypeDataReq;
import com.sluggard.security.manage.service.IDictDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author lizheng
 * @since 2024-05-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictDataEntity> implements IDictDataService {

    @Override
    public List<DictDataEntity> selectByDictType(DictTypeDataReq req) {
        return baseMapper.selectByDictType(req.getDictType());
    }

    @Override
    public void saveItems(String type, List<DictDataEntity> req) {
        // 删除已存在的
        baseMapper.deleteByDictType(req.get(0).getDictType());
        req.forEach(item -> item.setId(null));
        if(!CollectionUtils.isEmpty(req)){
            // 增加新的
            saveOrUpdateBatch(req);
        }

    }
}
